package sdufe.lele.controller;

import org.apache.shiro.util.ThreadContext;
import org.springframework.web.bind.annotation.*;
import sdufe.lele.annotation.authToken;
import sdufe.lele.dao.AccountTypeMapper;
import sdufe.lele.dao.BrokerMapper;
import sdufe.lele.dao.MessageMapper;
import sdufe.lele.dao.UserinfoMapper;
import sdufe.lele.model.AccountType;
import sdufe.lele.model.Broker;
import sdufe.lele.model.Message;
import sdufe.lele.model.Userinfo;
import sdufe.lele.result.ApiResult;
import sdufe.lele.result.MapResult;
import sdufe.lele.result.Result;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/*
 *
 * Created by zhnh9 on 4/12/2017.

 */


@RestController
@RequestMapping("/inoutcome")
public class InOutcomeController {

    @Resource
    private MessageMapper messageMapper;

    @Resource
    private AccountTypeMapper accountTypeMapper;

    @Resource
    private BrokerMapper brokerMapper;

    @Resource
    private UserinfoMapper userinfoMapper;


    /**
     * 记账时初始化，
     *
     * @return
     */
    @GetMapping("/init.do")
    @authToken
    public Result init(@RequestParam(name = "inout") Integer inout) {
        MapResult mapResult = new MapResult();
        String phone = (String)ThreadContext.get("user-phone");
        // 1代表收入
        List<AccountType> typeList = accountTypeMapper.selectInOutByPhone(inout,phone);
        // 没有参数，选择所有经办人
        List<Broker> brokerList = brokerMapper.selectAllByPhone(phone);
        mapResult.put("type", typeList);
        mapResult.put("broker", brokerList);
        return mapResult;


    }

    @PostMapping("/insertInOutcome.do")
    @authToken
    public Result insertIncome(@RequestParam(name = "mType") String mType,
                               @RequestParam(name = "mBroker") String mBroker,
                               @RequestParam(name = "mOthers") String mOthers,
                               @RequestParam(name = "inOut") String InOut,
                               @RequestParam(name = "mMoney") String mMoney) {


        ApiResult apiResult = new ApiResult();
        int inout = Integer.parseInt(InOut);
        String phone = (String) ThreadContext.get("user-phone");
        Message message = new Message(mType, mBroker, mMoney, mOthers, inout,phone);

        messageMapper.insertSelective(message);



        Userinfo ui = userinfoMapper.selectByPhone(phone);

        if (inout == 1) {
            userinfoMapper.updateBalance(ui.getBalance().add( new BigDecimal(mMoney)),phone);
        } else {
            userinfoMapper.updateBalance(ui.getBalance().subtract(new BigDecimal(mMoney)),phone);
        }


        return apiResult;


    }


    /**
     * ！！！！此方法废弃
     * 记录不能删除，只能补记
     *
     * @param id
     * @return
     */
    @Deprecated
    //@DeleteMapping("/deleteInOutcome.do")
    @authToken
    public Result deleteaIncome(@RequestParam(name = "id") String id) {
        ApiResult apiResult = new ApiResult();
        messageMapper.deleteByPrimaryKey(Integer.parseInt(id));
        return apiResult;

    }

    /**
     * ！！！！此方法废弃
     * 记录不能更新，只能补记
     *
     * @param mType
     * @param mBroker
     * @param mOthers
     * @param InOut
     * @param mMoney
     * @param id
     * @return
     */
    @Deprecated
    //@PutMapping("/updateInOutcome.do")
    @authToken
    public Result updateIncome(@RequestParam(name = "mType") String mType,
                               @RequestParam(name = "mBroker") String mBroker,
                               @RequestParam(name = "mOthers") String mOthers,
                               @RequestParam(name = "InOut") String InOut,
                               @RequestParam(name = "mMoney") String mMoney,
                               @RequestParam(name = "id") String id) {
        ApiResult apiResult = new ApiResult();
        Message message = new Message(mType, mBroker, mMoney, mOthers, Integer.parseInt(InOut),"");//这里可以为空
        message.setMessageid(Integer.parseInt(id));//这里需要id
        messageMapper.updateByPrimaryKey(message);
        return apiResult;

    }

    @GetMapping("/selectByPage.do")
    @authToken
    public Result selectIncomeByPage(@RequestParam(name = "page") String page) {
        MapResult mapResult = new MapResult();
        String phone = (String) ThreadContext.get("user-phone");
        List<Message> mlist;
        if (phone.equals("admin")) {
             mlist= messageMapper.selectByPage(Integer.parseInt(page), Integer.parseInt(page) * 100);
        }else {
            mlist = messageMapper.selectByPagAndPhone(Integer.parseInt(page), Integer.parseInt(page) * 100,phone);
        }
        mapResult.put("list", mlist);
        return mapResult;
    }

    @GetMapping("/selectById.do")
    @authToken
    public Result selectIncomeById(@RequestParam(name = "id") String id) {
        MapResult mapResult = new MapResult();
        Message message = messageMapper.selectByPrimaryKey(Integer.parseInt(id));
        mapResult.put("message", message);
        return mapResult;

    }

}
